<?php

namespace App\Http\Controllers\Admin;

use App\Models\Shop;
use App\Services\Service\Helper;
use Illuminate\Http\Request;

class ShopController extends BaseController
{
    //
    public function __construct()
    {
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $inputs = Helper::getInputs($request, ['opt_type' => '', 'keywords' => '',  'page' => 1, 'limit' => 10, 'status' => '']);

        $page = intval($inputs['page']);
        $limit = intval($inputs['limit']);
        $start = ($page - 1) * $limit;

        $where = [];
        $where['normal'][] = [];
        if ($inputs['opt_type'] != '' && $inputs['keywords'] != '') {
            if ($inputs['opt_type'] == 'id') {
                $where['where'][] = [$inputs['opt_type'] => $inputs['keywords']];
            } else {
                $where['where'][] = [$inputs['opt_type'], 'like', '%' . $inputs['keywords'] . '%'];
            }
        }
        if ($inputs['status'] != '') {
            $where['where'][] = ['status' => $inputs['status']];
        }
        $result = Shop::queryAll($where, [], [$start, $limit]);
        $result['conditions'] = $inputs;
        return $this->display('shop.index', $result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        return $this->display('shop.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        if ($request->ajax()) {
            $title = $request->input('title', '');
            if ($title == '') {
                return $this->error('请填写店铺名称');
            }
            $type = $request->input('type', 1);
            if (!$type) {
                return $this->error('请选择店铺类型');
            }
            $sid = $request->input('sid', 0);
            if ($sid == '') {
                return $this->error('填写店铺SID');
            }
            $seller_name = $request->input('seller_name', 0);
            if ($seller_name == '') {
                return $this->error('填写卖家名称');
            }
            $shop = new Shop();
            $shop->type = $type;//商户类型1淘宝2天猫
            $shop->title = $title;
            $shop->sid = $sid;
            $shop->seller_name = $seller_name;
            $shop->desc = $request->input('desc', '');
            if (false !== $shop->save()) {
                return $this->success('新增成功', ['redirect' => url('shop')]);
            } else {
                return $this->error('新增失败');
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $shop = Shop::find($id);
        $this->assign('vo', $shop);
        return $this->display('shop.show');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $shop = Shop::find($id);
        $this->assign('vo', $shop);
        return $this->display('shop.edit');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if ($request->ajax()) {
            $shop = Shop::find($id);
            if (is_null($shop)) {
                return $this->error('查找店铺信息失败');
            }
            $title = $request->input('title', '');
            if ($title == '') {
                return $this->error('请填写店铺名称');
            }
            //
            $type = $request->input('type', 1);
            if (!$type) {
                return $this->error('请选择店铺类型');
            }
            $sid = $request->input('sid', 0);
            if ($sid == '') {
                return $this->error('填写店铺SID');
            }
            $seller_name = $request->input('seller_name', 0);
            if ($seller_name == '') {
                return $this->error('填写卖家名称');
            }
            $shop->type = $type;
            $shop->title = $title;
            $shop->sid = $sid;
            $shop->seller_name = $seller_name ;
            $shop->desc = $request->input('desc', '');
            if (false !== $shop->save()) {
                return $this->success('更新成功', ['redirect' => url('shop')]);
            } else {
                return $this->error('更新失败');
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        if ($request->ajax()) {
            $shop = Shop::find($id);
            if (is_null($shop)) {
                return $this->error('查找店铺信息失败');
            }
            if ($shop->products->count()) {
                return $this->error('店铺下存在宝贝，不能删除');
            }
            $shop->is_delete = 1;
            if (false !== $shop->save()) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        }
    }

    /**
     * 店铺状态调整
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function switch_status(Request $request)
    {
        if ($request->ajax()) {
            $id = $request->input('id', 0);
            $value = $request->input('value', 2);
            if ($value == 2) {
                $value = 1;
            } else {
                $value = 2;
            }
            $shop = Shop::find($id);
            if (is_null($shop)) {
                return $this->error('店铺不存在');
            } else {
                $shop->status = $value;
                $shop->save();
                return $this->success('success', ['value' => $value]);
            }
        }
    }
}
